import socket
import scapy
from scapy.all import sniff
from scapy.all import rdpcap

def Callback(packet):
    print('src:%s----->dst:%s' % (packet[IP].src, packet[IP].dst))
    print('TTL:%s' % packet[IP].ttl)
    print(packet.show())  # 内置的show()函数打印数据包内容


def count_packets(pkt):
    global icmp_count
    if pkt.haslayer('ICMP'):
        icmp_count += 1



def start_client(host='192.168.1.5', port=2332):
    # 创建一个 TCP/IP socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # 连接到服务器
        s.connect((host, port))
        # 发送数据
        message = "Hello, Server! I am Client"
        s.sendall(message.encode('utf-8'))
        #sendall可以一次性全部传完，
        #send每次只能传输MTU的大小，剩余丢弃，需手工循环

        # #发送大文件，分块发送
        # file = open("data.bin", "rb")
        # while True:
        #     data = file.read(1024)
        #     if not data:
        #         break
        #     conn.sendall(data)
        # file.close()
        # print("文件传输完成！")
        # 接收回送的数据
        data = s.recv(1024)
        print(f"从服务器接收到: {data.decode('utf-8')}")
        s.close()





if __name__ == "__main__":
    start_client()
    #抓包
    #监听 WLAN 接口的流量包（网卡名可以在 更改适配器选项 里面看），监听时间是 60s
    # filter = 'src host 192.168.1.5 && dst port 2332'
    # pkt = sniff(iface='Red Hat VirtIO Ethernet Adapter #2', filter=filter, timeout=60, prn=count_packets)
    # print(pkt)
















